package com.zsc.shixun.service.function.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.zsc.shixun.entity.Article;
import com.zsc.shixun.entity.Comment;
import com.zsc.shixun.model.unauthorize.article.ArticleCommentCollectVO;
import com.zsc.shixun.service.IArticleService;
import com.zsc.shixun.service.ICollectCategoriesService;
import com.zsc.shixun.service.ICommentService;
import com.zsc.shixun.service.IUserService;
import com.zsc.shixun.service.function.ArticleCommentCollectService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

/**
 * <p>
 * 文章页
 * </p>
 *
 * @author ZWYZY
 * @since 2020/6/21
 */
@Service
@Transactional
public class ArticleCommentCollectServiceImpl implements ArticleCommentCollectService {
    @Autowired
    private IArticleService articleService;

    @Autowired
    private ICommentService commentService;
    @Autowired
    private ICollectCategoriesService collectCategoriesService;
    @Autowired
    private IUserService userService;

    @Override
    public ArticleCommentCollectVO getArticle(Long id) {
        ArticleCommentCollectVO articleCommentCollectVO = new ArticleCommentCollectVO();
        //博主id
        Long userId = articleService.getById(id).getUserId();
        articleCommentCollectVO.setUser(userService.getById(userId));
        articleCommentCollectVO.setCommentList(commentService.list(new QueryWrapper<Comment>().lambda().eq(Comment::getArticleId, id)));
        Article article = articleService.getById(id);
        article.setClickNumber(article.getClickNumber() + 1);
        articleService.updateById(article);
        articleCommentCollectVO.setArticle(article);
        LambdaQueryWrapper<Article> lambdaQueryWrapper = new QueryWrapper<Article>().lambda().eq(Article::getUserId, userId);
        //博主总文章页量
        articleCommentCollectVO.setArticleNumber(Long.valueOf(articleService.count(lambdaQueryWrapper)));
        //博主所有文章总访问量
        articleCommentCollectVO.setClickNumber(articleService.list(lambdaQueryWrapper).stream().mapToLong(m -> m.getClickNumber()).sum());
        return articleCommentCollectVO;

    }
}
